var moment=require('moment');
var mongoose=require('mongoose');

var NoteShema=new mongoose.Schema({
    title:String,
    createTime:Date,//创建时间
    expirationTime:Date,//过期时间
    desc:String,
    status:{
        type:Number,
        default:0,
        enum:[-1,0,1]//-1:已过期;0:未完成;1:已完成
    }
});
NoteShema.statics.get=function(query,cb){
    console.info('\n----------------------------get:');
    console.log('query:',query);
    var cbQuery={};
    var start=Number(query.start);
    var end=Number(query.end);
    var status=Number(query.status);


    var expirationTime={};
    query={};
    if(start){
        expirationTime.$gte=start;
        cbQuery.start=start;
    }
    if(end){
        expirationTime.$lte=end;
        cbQuery.end=end;
    }
    if([-1,0,1].includes(status)){
        query.status=status;
        cbQuery.status=status;
    }
    if(start || end){
        query.expirationTime=expirationTime;
    }
    console.log('find:',query);
    this.count(query,(err,count)=>{
        if(err)return cb(err);
        this.find(query,{},(err,ret)=>{
            if(err)return cb(err);
            cb(null,ret);
        });
    });
};

NoteShema.virtual('createTimeString').get(function (){
    return moment(this.createTime).format('YYYY-MM-DD HH:mm:ss');
});
NoteShema.virtual('expirationTimeString').get(function () {
    return moment(this.expirationTime).format('YYYY-MM-DD HH:mm:ss');
});
NoteShema.virtual('statusString').get(function () {
    return ({'-1':'已过期','0':'未完成','1':'已完成'})[this.status];
});
module.exports=mongoose.model('Note',NoteShema);